﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using QuaLuuNiem.Models;
namespace QuaLuuNiem.Controllers
{
    public class QuaLuuNiemController : Controller
    {
        private DOAN_QUA_LUU_NIEMEntities db = new DOAN_QUA_LUU_NIEMEntities();
        //
        // GET: /QuaLuuNiem/

        public ActionResult Index()
        {
            ViewBag.MenuRight = from s in db.MENURIGHTs select s;
            ViewBag.SanPhamKM = (from s in db.SANPHAMs where s.TrangThai == 4 select s);
            ViewBag.SanPhamMoi = (from s in db.SANPHAMs orderby s.MaSP descending select s).Take(10);
            ViewBag.TinMoiNhat = (from s in db.BAIVIETs orderby s.MaBaiViet descending select s).Take(10);
            return View();
        }

        public ActionResult lien_he()
        {
            ViewBag.MenuRight = from s in db.MENURIGHTs select s;
            return View();
        }
        public ActionResult san_pham(int? id,string currentFilter, string key, int? page)
        {
            ViewBag.MenuRight = from s in db.MENURIGHTs  select s;
            if (Request.HttpMethod == "GET")
            {
                key = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = key;
            var sanpham = from s in db.SANPHAMs select s;
            if (id.ToString() != "")
            {
                sanpham = sanpham.Where(s => s.MaMenu == id);
            }
            
            if (!String.IsNullOrEmpty(key))
            {
                sanpham = sanpham.Where(s => s.TenSP.ToUpper().Contains(key.ToUpper()));
            }
            sanpham = sanpham.OrderBy(s => s.MaSP);
            int pageSize = 8;
            int pageNumber = (page ?? 1);
            return View(sanpham.ToPagedList(pageNumber, pageSize));
        }
       
        public ActionResult bai_viet(string currentFilter, string key, int? page)
        {
            ViewBag.MenuRight = from s in db.MENURIGHTs select s;
            if (Request.HttpMethod == "GET")
            {
                key = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = key;
            var baiviet = from s in db.BAIVIETs select s;
            if (!String.IsNullOrEmpty(key))
            {
                baiviet = baiviet.Where(s => s.TenBaiViet.ToUpper().Contains(key.ToUpper()));
            }
            baiviet = baiviet.OrderBy(s => s.MaBaiViet);
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(baiviet.ToPagedList(pageNumber, pageSize));
        }

        public ActionResult ChiTietBaiViet(int id)
        {
            ViewBag.MenuRight = from s in db.MENURIGHTs select s;
            ViewBag.SoBLBV = (from bl in db.BINHLUANBAIVIETs where bl.MaBaiViet == id select bl.NoiDung).Count();
            ViewBag.BinhLuanBV = from bl in db.BINHLUANBAIVIETs where bl.MaBaiViet == id orderby bl.MaBL descending select bl;
            BAIVIET bv = db.BAIVIETs.Single(s => s.MaBaiViet == id);
            return View(bv);
        }
        [HttpPost]
        public ActionResult ChiTietBaiViet(BINHLUANBAIVIET blbv,int id)
        {
            ViewBag.MenuRight = from s in db.MENURIGHTs select s;
            BAIVIET bv = db.BAIVIETs.Single(s => s.MaBaiViet == id);
            if (ModelState.IsValid)
            {
                db.BINHLUANBAIVIETs.AddObject(blbv);
                blbv.MaBaiViet = id;
                blbv.NgayBL = DateTime.Now;
                db.SaveChanges();
                return RedirectToAction("ChiTietBaiViet");
            }
            return View(blbv);
        }

        public ActionResult chi_tiet_tai_khoan()
        {
            ViewBag.MenuRight = from s in db.MENURIGHTs select s;
            return View();
        }
        public ActionResult chi_tiet_san_pham(int id)
        {
            ViewBag.MenuRight = from s in db.MENURIGHTs select s;
            ViewBag.SoBL = (from bl in db.BINHLUANs where bl.MaSP == id select bl.NoiDung).Count();
            ViewBag.BinhLuanSanPham = from bl in db.BINHLUANs where bl.MaSP==id orderby bl.maBL descending  select bl;
            SANPHAM sanpham = db.SANPHAMs.Single(s => s.MaSP == id);
            return View(sanpham);
        }
        [HttpPost]
        public ActionResult chi_tiet_san_pham(BINHLUAN binhluan,int id)
        {
            ViewBag.MenuRight = from s in db.MENURIGHTs select s;
            ViewBag.BinhLuanSanPham = from bl in  db.BINHLUANs  where bl.MaSP == id select bl;
            if (ModelState.IsValid)
            {
                db.BINHLUANs.AddObject(binhluan);
                binhluan.MaSP = id;
                binhluan.NgayBL = DateTime.Now;
                db.SaveChanges();
                return RedirectToAction("chi_tiet_san_pham");
            }
            return View(binhluan);
        }

        public ActionResult dang_nhap_dang_ky()
        {
            ViewBag.MenuRight = from s in db.MENURIGHTs select s;
            return View();
        }
        [HttpPost]
        public ActionResult dang_nhap_dang_ky(KHACHHANG dangky)
        {
            ViewBag.MenuRight = from s in db.MENURIGHTs select s;
            if (ModelState.IsValid)
            {
                dangky.DiemMuaHang = 0;
                db.KHACHHANGs.AddObject(dangky);
                db.SaveChanges();
                return RedirectToAction("DangKyThanhCong",dangky);
            }

            return View(dangky);
        }

        public ActionResult DangKyThanhCong(KHACHHANG dangky)
        {
            ViewBag.MenuRight = from s in db.MENURIGHTs select s;
            return View(dangky);
        }
        public ActionResult gio_hang()
        {
            ViewBag.MenuRight = from s in db.MENURIGHTs select s;
            return View();
        }

    }
}
